package org.jeecg.modules.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.inventory.entity.BussInventory;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.entity.BussInventoryInoutDetails;
import org.jeecg.modules.inventory.mapper.BussInventoryDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryInoutDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryMapper;
import org.jeecg.modules.inventory.service.IBussInventoryService;
import org.jeecg.modules.inventory.vo.BussInventoryPage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 库存管理
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Service
public class BussInventoryServiceImpl extends ServiceImpl<BussInventoryMapper, BussInventory> implements IBussInventoryService {

	@Autowired
	private BussInventoryMapper bussInventoryMapper;
	@Autowired
	private BussInventoryDetailsMapper bussInventoryDetailsMapper;
	@Autowired
	private BussInventoryInoutDetailsMapper bussInventoryInoutDetailsMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussInventory bussInventory, List<BussInventoryDetails> bussInventoryDetailsList,List<BussInventoryInoutDetails> bussInventoryInoutDetailsList) {
		bussInventoryMapper.insert(bussInventory);
		if(bussInventoryDetailsList!=null && bussInventoryDetailsList.size()>0) {
			for(BussInventoryDetails entity:bussInventoryDetailsList) {
				//外键设置
				entity.setInventoryId(bussInventory.getId());
				bussInventoryDetailsMapper.insert(entity);
			}
		}
		if(bussInventoryInoutDetailsList!=null && bussInventoryInoutDetailsList.size()>0) {
			for(BussInventoryInoutDetails entity:bussInventoryInoutDetailsList) {
				//外键设置
				entity.setInventoryId(bussInventory.getId());
				bussInventoryInoutDetailsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussInventory bussInventory,List<BussInventoryDetails> bussInventoryDetailsList,List<BussInventoryInoutDetails> bussInventoryInoutDetailsList) {
		bussInventoryMapper.updateById(bussInventory);
		
		//1.先删除子表数据
		bussInventoryDetailsMapper.deleteByMainId(bussInventory.getId());
		bussInventoryInoutDetailsMapper.deleteByMainId(bussInventory.getId());
		
		//2.子表数据重新插入
		if(bussInventoryDetailsList!=null && bussInventoryDetailsList.size()>0) {
			for(BussInventoryDetails entity:bussInventoryDetailsList) {
				//外键设置
				entity.setInventoryId(bussInventory.getId());
				bussInventoryDetailsMapper.insert(entity);
			}
		}
		if(bussInventoryInoutDetailsList!=null && bussInventoryInoutDetailsList.size()>0) {
			for(BussInventoryInoutDetails entity:bussInventoryInoutDetailsList) {
				//外键设置
				entity.setInventoryId(bussInventory.getId());
				bussInventoryInoutDetailsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bussInventoryDetailsMapper.deleteByMainId(id);
		bussInventoryInoutDetailsMapper.deleteByMainId(id);
		bussInventoryMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bussInventoryDetailsMapper.deleteByMainId(id.toString());
			bussInventoryInoutDetailsMapper.deleteByMainId(id.toString());
			bussInventoryMapper.deleteById(id);
		}
	}

	@Override
	public List<BussInventoryPage> getByIds(List<String> ids,String warehouseId) {
		String[] idArray = ids.stream().toArray(String[]::new);
		List<BussInventoryPage> list = bussInventoryMapper.selectByIds(idArray,warehouseId);
		//查询库存明细
		for (BussInventoryPage inventory: list){
			inventory.setBussInventoryDetailsList(bussInventoryDetailsMapper.selectByMainIdOrderByTime(inventory.getMaterialId(),warehouseId));
		}
		return list;
	}

	@Override
	public IPage<BussInventory> myPage(Page<BussInventory> page, QueryWrapper<BussInventory> queryWrapper) {
		return bussInventoryMapper.myPage(page,queryWrapper);
	}

	@Override
	public IPage<BussInventory> myInventoryPage(Page<BussInventory> page, BussInventory bussInventory) {
		return bussInventoryMapper.myInventoryPage(page,bussInventory);
	}
}
